package com.xjscrm.console.service.common.sysNotice.impl;

import cn.hutool.core.bean.BeanUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xjscrm.common.constant.ResponseMsg;
import com.xjscrm.common.entity.SysNotice;
import com.xjscrm.common.exception.BusinessException;
import com.xjscrm.common.result.ResponseCode;
import com.xjscrm.common.utils.OrderByUtil;
import com.xjscrm.console.mapper.SysNoticeMapper;
import com.xjscrm.console.param.sysNotice.SysNoticeDto;
import com.xjscrm.console.param.sysNotice.SysNoticeQuery;
import com.xjscrm.console.service.common.sysNotice.SysNoticeService;
import com.xjscrm.console.vo.MyPageInfo;
import com.xjscrm.console.vo.SysNoticeVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class SysNoticeServiceImpl implements SysNoticeService {

    @Autowired
    private SysNoticeMapper sysNoticeMapper;
    @Override
    public void addSysNotice(SysNoticeDto sysNoticeDto) {
        if(sysNoticeDto.getMerId()==null){
            throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION, ResponseMsg.MERID_NULL);
        }
        if(StringUtils.isBlank(sysNoticeDto.getNoticeTitle())){
            throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION, ResponseMsg.PARAMETER_ERROR);
        }
        if(StringUtils.isBlank(sysNoticeDto.getNoticeContent())){
            throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION, ResponseMsg.PARAMETER_ERROR);
        }
        if(sysNoticeDto.getNoticeFlag()==null){
            throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION, ResponseMsg.PARAMETER_ERROR);
        }
        if(sysNoticeDto.getNoticeType()==null){
            throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION, ResponseMsg.PARAMETER_ERROR);
        }
        SysNotice sysNotice = new SysNotice();
        BeanUtil.copyProperties(sysNoticeDto,sysNotice);
        sysNotice.setCreateUserId(sysNoticeDto.getMerId());
        sysNoticeMapper.addSysNotice(sysNotice);
    }

    @Override
    public Object getSysNotice(SysNoticeQuery sysNoticeQuery) {
        String orderBy = OrderByUtil.getOrderBy(sysNoticeQuery.getOrderByField(), sysNoticeQuery.isAsc());
        PageHelper.startPage(sysNoticeQuery.getCurrent(),sysNoticeQuery.getSize(),orderBy);
        List<SysNoticeVo> sysNoticeVoList= sysNoticeMapper.getSysNotice(sysNoticeQuery);
        MyPageInfo<SysNoticeVo> pageInfo = new MyPageInfo<>(sysNoticeVoList);
        return pageInfo;
    }
}
